package com.itheima.edu.info.managers.controller;

import com.itheima.edu.info.managers.domain.Teacher;
import com.itheima.edu.info.managers.service.TeacherService;

import java.util.Scanner;

public class TeacherController {
    private Scanner sc = new Scanner(System.in);
    private TeacherService teacherService = new TeacherService();

    public void start() {
        lo:
        while (true) {
            System.out.println("--------欢迎来到 <教师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加教师  2.删除教师   3.修改教师   4.查看教师   5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    addTeacher();
                    break;
                case "2":
                    //System.out.println("删除");
                    deleteTeacher();
                    break;
                case "3":
                    //System.out.println("修改");
                    updateTeacher();
                    break;
                case "4":
                    //System.out.println("查看");
                    findAllTeacher();
                    break;
                case "5":
                    System.out.println("感谢使用教师信息管理系统，再见");
                    break lo;
                default:
                    System.out.println("你的输入有误，请重新输入");
                    break;
            }
        }
    }

    public void updateTeacher() {
        String updateId = inputTeacherId();
        Teacher tea = inputTeacherInfo(updateId);
        teacherService.updateTeacher(tea,updateId);

    }

    public void findAllTeacher() {
        // 1. 调用业务员中的获取方法, 得到学生的对象数组
        Teacher[] stus =teacherService.findAllTeacher();
        // 2. 判断数组的内存地址, 是否为null
        if (stus == null) {
            System.out.println("查无信息，请输入后再试");
            return;
        }
        // 3. 遍历数组, 获取学生信息并打印在控制台
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < stus.length; i++) {
            Teacher stu = stus[i];
            if (stu != null) {
                System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t\t" + stu.getBirthday());
            }
        }
    }

    public void deleteTeacher() {
        //键盘录入学生学号，
        String delId = inputTeacherId();
        teacherService.deleteTeacher(delId);
        System.out.println("删除成功");
    }

    public void addTeacher() {

        //键盘录入需要封装的学生数据
        String id;
        while (true) {
            System.out.println("请输入学生的学号");
            id = sc.next();
            boolean flag = teacherService.isExists(id);
            if (flag) {
                System.out.println("学号已存在，请重新输入");
            } else {
                break;
            }
        }
        //调用封装数据的方法
        Teacher tea = inputTeacherInfo(id);
        //接受StudentService的返回值 且判断是否添加成功
        boolean flag = teacherService.addTeacher(tea);
        if (flag) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }


    }

    public Teacher inputTeacherInfo(String id) {
        System.out.println("请输入学生的姓名");
        String name = sc.next();
        System.out.println("请输入学生的年龄");
        String age = sc.next();
        System.out.println("请输入学生的生日");
        String birthday = sc.next();
        Teacher stu = new Teacher();
        stu.setId(id);
        stu.setName(name);
        stu.setAge(age);
        stu.setBirthday(birthday);
        return stu;
    }

    public String  inputTeacherId(){
        String id;
        while (true) {
            System.out.println("请输入学生学号：");
            id = sc.next();
            boolean flag = teacherService.isExists(id);
            if (flag) {
                break;
            } else {
                System.out.println("你输入的学号不存在");
            }
        }
        return id;
    }
}
